package com.google.servicedemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button close_service;
    private Button open_service;
    private Button bind_service;
    private Button unbind_service;
    private TextView tv;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            Log.e("mrgao","MainActivity onServiceConnected()");
//            MyService.MyBind myBind = (MyService.MyBind) iBinder;
//            myBind.startDownLoad();
            IMyAidlInterface aidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                int plus = aidlInterface.plus(3, 5);
                String toUpperCase = aidlInterface.toUpperCase("hello world");

                Log.e("mrgao","MainActivity plus:"+plus);
                Log.e("mrgao","MainActivity toUpperCase:"+toUpperCase);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

            Log.e("mrgao","MainActivity onServiceDisconnected()");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.e("mrgao", "process id is " + Process.myPid());
        close_service = (Button) findViewById(R.id.close_service);
        open_service = (Button) findViewById(R.id.open_service);
        bind_service = (Button) findViewById(R.id.bind_service);
        unbind_service = (Button) findViewById(R.id.unbind_service);
        tv = (TextView) findViewById(R.id.tv_textview2);

        close_service.setOnClickListener(this);
        open_service.setOnClickListener(this);
        bind_service.setOnClickListener(this);
        unbind_service.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.open_service:
                Intent intent = new Intent(this, MyService.class);
               startService(intent);
                break;
            case R.id.close_service:
                Intent stopIntent = new Intent(this, MyService.class);
                stopService(stopIntent);
                break;
            case R.id.bind_service:
                Intent bindservice = new Intent(this, MyService.class);
                bindService(bindservice,conn,BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(conn);
                break;
        }
    }
}
